package com.example.ai.inventory.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * 进销存系统工具类
 */
public class InventoryUtils {
    
    private static final Random random = new Random();
    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 生成采购单号
     */
    public static String generatePurchaseOrderNo() {
        String dateStr = LocalDateTime.now().format(dateFormatter);
        int randomNum = 10000 + random.nextInt(90000); // 生成5位随机数
        return "PO" + dateStr + randomNum;
    }
    
    /**
     * 生成销售单号
     */
    public static String generateSalesOrderNo() {
        String dateStr = LocalDateTime.now().format(dateFormatter);
        int randomNum = 10000 + random.nextInt(90000); // 生成5位随机数
        return "SO" + dateStr + randomNum;
    }
    
    /**
     * 格式化日期时间
     */
    public static String formatDateTime(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.format(datetimeFormatter);
    }
    
    /**
     * 解析日期时间字符串
     */
    public static LocalDateTime parseDateTime(String dateTimeStr) {
        if (dateTimeStr == null || dateTimeStr.isEmpty()) {
            return null;
        }
        return LocalDateTime.parse(dateTimeStr, datetimeFormatter);
    }
    
    /**
     * 生成商品编码
     */
    public static String generateProductCode() {
        String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
        int randomNum = 1000 + random.nextInt(9000); // 生成4位随机数
        return "PROD" + dateStr + randomNum;
    }
    
    /**
     * 校验金额格式
     */
    public static boolean isValidAmount(double amount) {
        return amount >= 0 && Double.isFinite(amount);
    }
    
    /**
     * 校验数量格式
     */
    public static boolean isValidQuantity(int quantity) {
        return quantity >= 0;
    }
}